<?php

namespace Modules\BlogAdmin\Models;
use Modules\Admin\Models\BaseModel;
use Illuminate\Support\Facades\DB;
class BlogArticle extends BaseModel
{	
	
	/**
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\Admin\Models\Image','id','image_id');
    }
	/**
     * @name  关联栏目
     */
    public function column_to()
    {
        return $this->belongsTo('Modules\BlogAdmin\Models\BlogColumn','blog_column_id','id');
    }
	/**
     * @name  关联评论
     */
    public function comment_many()
    {
        return $this->hasMany('Modules\BlogAdmin\Models\BlogComment','blog_article_id','id');
    }
	
	/**
     * @name  关联标签
     */
    public function label_to()
    {
        return $this->belongsToMany('Modules\BlogAdmin\Models\BlogLabel', 'blog_article_labels', 'blog_article_id', 'blog_label_id')->withPivot(['blog_article_id', 'blog_label_id']);
    }
	
	/**
     * @name  数据添加
     */
    public function add_create($data,$url)
    {
		DB::beginTransaction();
		try{
			$data['created_at'] = date('Y-m-d H:i:s');
			if(isset($data['blog_label_id'])){
				$blog_label_id = $data['blog_label_id'];
				unset($data['blog_label_id']);
			}
			$id = $this->insertGetId($data);
			if(isset($blog_label_id) && count($blog_label_id) > 0){
				foreach($blog_label_id as $v){
					$article_labels[] = ['blog_article_id'=>$id,'blog_label_id'=>$v]; 
				}  
				DB::table('blog_article_labels')->insert($article_labels);	
			}	
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return response()->json([
				'status'=>0,
				'error' => config('admin.create_error'),
				'msg' => '',
				'url'=>$url
			],201);
		}
		return response()->json([
			'status'=>1,
			'error' => '',
			'msg' => config('admin.create_success'),
			'url'=>$url
		],200);	
    }
	/**
     * @name  数据编辑
     */
    public function update_article($data,$id,$url)
    {
		DB::beginTransaction();
		try{
			if(isset($data['blog_label_id'])){
				$blog_label_id = $data['blog_label_id'];
				unset($data['blog_label_id']);
			}
			unset($data['_method']);
			$data['updated_at'] = date('Y-m-d H:i:s');
			$this->where('id',$id)->update($data);
			$this->article_labels_del([$id]); 
			if(isset($blog_label_id) && count($blog_label_id) > 0){
				foreach($blog_label_id as $v){
					$article_labels[] = ['blog_article_id'=>$id,'blog_label_id'=>$v]; 
				}  
				DB::table('blog_article_labels')->insert($article_labels);	
			}	
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return response()->json([
				'status'=>0,
				'error' => config('admin.update_error'),
				'msg' => '',
				'url'=>$url
			],201);
		}
		return response()->json([
			'status'=>1,
			'error' => '',
			'msg' => config('admin.update_success'),
			'url'=>$url
		],200);	 
    }
	public function article_labels_del($id){
		$article_labels_arr = [];
		foreach($id as $v1){
			$article_labels_arr = array_merge($article_labels_arr,DB::table('blog_article_labels')->where(['blog_article_id'=>$v1])->get()->toArray()); 
		}
		foreach($article_labels_arr as $v){
			DB::table('blog_article_labels')->where(['id'=>$v->id])->delete();
		}
	}
	/**
     * @name  列表数据处理
     */
	public function data_image($data){ 
		foreach($data as $k=>$v){
			$data[$k]['column'] = $v['column_to']['title'];
			if($v['image_one']['status'] == 1){
				$data[$k]['image'] = $v['image_one']['url'];
			}else{
				$data[$k]['image'] = '';
			}
		}
		return $data;
	}
	
	
	
	
	
	
}
